package com.example.blog.service.Impl;

import com.example.blog.config.page.PageRequest;
import com.example.blog.entity.Tag;
import com.example.blog.mapper.TagMapper;
import com.example.blog.service.TagService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lu
 * @Date: 2024-01-15-16:58
 * @Description:
 */
@Service
public class TagServiceImpl implements TagService {
    @Autowired
    TagMapper tagMapper;

    @Autowired
    Tag tag;

    @Override
    public List<Tag> find(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        List<Tag> tags = tagMapper.find();
        return tags;
    }

    @Override
    public void create(Tag tag) {
        tagMapper.create(tag);

    }

    @Override
    public int delete(Integer id) {
        int delete = tagMapper.delete(id);
        return delete;
    }

    @Override
    public void update(Tag tag) {
        tagMapper.update(tag);

    }

    @Override
    public boolean batchAddTag(String tags) throws Exception {
        //将字符串转成数组
        String[] split = tags.split(",");
        List<Tag> tagList = new ArrayList<>();

        for (String str :split){
            tag.setTagName(str);
            tagList.add(tag);
        }

        if(tagList == null || tagList.size() ==0 || tagList.size() > 10){
            throw  new Exception("超过添加标签的限制");
        }
        boolean isStatus = tagMapper.batchAddTag(tagList);
        return isStatus;
    }

    @Override
    public boolean batchDeleteTag(String ids) {
        return false;
    }
}
